/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 12.09.12
 * Time: 10:14
 * To change this template use File | Settings | File Templates.
 */
package fgui.content {
import fgui.GuiComponent;

import fgui.IAnimationComponent;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.media.Sound;
import flash.media.Video;

import mx.controls.Image;
import mx.controls.VSlider;

/**
 * @link ContentContainer
 *
 * @description You can use this component like a panel for content with fixed width and height.
 *              For viewing data you can use VSlider component.
 */
public class ContentContainer extends GuiComponent implements IAnimationComponent {
    private var content:GuiComponent;
    private var maskForContent:GuiComponent;
    private var vslider:VSlider;

    public function ContentContainer(coordinate:Point, size:Point) {
        super(coordinate, size);

        renderColor = 0x999999;
        renderAlpha = 1;
        renderDefault();

        this.content = new GuiComponent(
                new Point(margin, margin),
                new Point(this.bgWidth - 2 * margin, 2 * this.bgHeight)
                );
        content.renderCustom(0xfcfcfc);
        addChild(content);

        this.maskForContent = new GuiComponent(
                new Point(margin, margin),
                new Point(this.bgWidth - 2 * margin, this.bgHeight - 2 * margin)
                );
        addChild(maskForContent);
        content.mask = maskForContent;

        vslider = new VSlider();
        vslider.x = maskForContent.getSize().x - margin;
        vslider.y = margin;
        vslider.width = margin;
        vslider.height = maskForContent.getSize().y;
        vslider.minimum = -100;
        vslider.maximum = 0;
        vslider.value = 0;
        addChild(vslider);

        this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
        this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
        this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    }

    public function addText(coordinate:Point, size:Point, sourceText:String):void {
        this.content.addChild(new TextContainer(coordinate, size, sourceText));
    }

    public function addImage(coordinate:Point, size:Point, sourceImage:Image):void {
        //
    }

    public function addAudio(coordinate:Point, size:Point, sourceAudio:Sound):void {
        //
    }

    public function addVideo(coordinate:Point, size:Point, sourceVideo:Video):void {
        //
    }

    /* Interface IAnimationComponent */
    public function renderOnMouseOver(event:MouseEvent):void {
    }

    public function renderOnMouseDown(event:MouseEvent):void {
    }

    public function renderOnMouseOut(event:MouseEvent):void {
    }

    /* /Interface IAnimationComponent */

    private var oldVSliderValue:Number = 1000;

    private function onEnterFrame(event:Event):void {
        if (oldVSliderValue == 1000) {
            oldVSliderValue = vslider.value;
        }
        if (vslider.value != oldVSliderValue) {
            content.y += 2 * (vslider.value - oldVSliderValue);
            oldVSliderValue = vslider.value;
        }
    }

    private function onMouseOver(event:MouseEvent):void {
        stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
    }

    private function onMouseWheel(event:MouseEvent):void {
        content.y += 5 * event.delta;
    }

    private function onMouseOut(event:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
    }
}
}
